{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Description：\n",
    "这里分别用FM演示分类和回归问题如何通过调包来完成任务。 回归任务这里依然采用的协同过滤里面的评分数据集， 分类任务， 这里先采用自动生成的一个数据集，造轮子的时候， 会采用kaggle上的一个ctr数据集进行实战"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 回归任务\n",
    "回归任务的数据依然是电影评分数据集， 数据集的下载地址: [ http://www.grouplens.org/system/files/ml-100k.zip](http://www.grouplens.org/system/files/ml-100k.zip)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T13:38:44.757014Z",
     "start_time": "2020-09-25T13:38:44.129693Z"
    }
   },
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import numpy as np\n",
    "from sklearn.feature_extraction import DictVectorizer\n",
    "from pyfm import pylibfm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T11:53:00.280889Z",
     "start_time": "2020-09-25T11:53:00.275860Z"
    }
   },
   "outputs": [],
   "source": [
    "# 导入数据\n",
    "def loadData(filename, path='ml-100k/'):\n",
    "    data = []\n",
    "    y = []\n",
    "    users = set()\n",
    "    items = set()\n",
    "    with open(path+filename) as f:\n",
    "        for line in f:\n",
    "            (user, movieid, rating, ts) = line.split('\\t')\n",
    "            data.append({'user_id': str(user), 'movie_id': str(movieid)})\n",
    "            y.append(float(rating))\n",
    "            users.add(user)\n",
    "            items.add(movieid)\n",
    "    \n",
    "    return (data, np.array(y), users, items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T11:54:22.804066Z",
     "start_time": "2020-09-25T11:54:22.671377Z"
    }
   },
   "outputs": [],
   "source": [
    "# 导入数据\n",
    "(train_data, y_train, train_users, train_items) = loadData('ua.base')\n",
    "(test_data, y_test, test_users, test_items) = loadData('ua.test')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T12:35:55.722748Z",
     "start_time": "2020-09-25T12:35:55.658163Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'user_id': '1', 'movie_id': '1'},\n",
       " {'user_id': '1', 'movie_id': '2'},\n",
       " {'user_id': '1', 'movie_id': '3'},\n",
       " {'user_id': '1', 'movie_id': '4'},\n",
       " {'user_id': '1', 'movie_id': '5'},\n",
       " {'user_id': '1', 'movie_id': '6'},\n",
       " {'user_id': '1', 'movie_id': '7'},\n",
       " {'user_id': '1', 'movie_id': '8'},\n",
       " {'user_id': '1', 'movie_id': '9'},\n",
       " {'user_id': '1', 'movie_id': '10'},\n",
       " {'user_id': '1', 'movie_id': '11'},\n",
       " {'user_id': '1', 'movie_id': '12'},\n",
       " {'user_id': '1', 'movie_id': '13'},\n",
       " {'user_id': '1', 'movie_id': '14'},\n",
       " {'user_id': '1', 'movie_id': '15'},\n",
       " {'user_id': '1', 'movie_id': '16'},\n",
       " {'user_id': '1', 'movie_id': '17'},\n",
       " {'user_id': '1', 'movie_id': '18'},\n",
       " {'user_id': '1', 'movie_id': '19'},\n",
       " {'user_id': '1', 'movie_id': '21'},\n",
       " {'user_id': '1', 'movie_id': '22'},\n",
       " {'user_id': '1', 'movie_id': '23'},\n",
       " {'user_id': '1', 'movie_id': '24'},\n",
       " {'user_id': '1', 'movie_id': '25'},\n",
       " {'user_id': '1', 'movie_id': '26'},\n",
       " {'user_id': '1', 'movie_id': '27'},\n",
       " {'user_id': '1', 'movie_id': '28'},\n",
       " {'user_id': '1', 'movie_id': '29'},\n",
       " {'user_id': '1', 'movie_id': '30'},\n",
       " {'user_id': '1', 'movie_id': '31'},\n",
       " {'user_id': '1', 'movie_id': '32'},\n",
       " {'user_id': '1', 'movie_id': '34'},\n",
       " {'user_id': '1', 'movie_id': '35'},\n",
       " {'user_id': '1', 'movie_id': '36'},\n",
       " {'user_id': '1', 'movie_id': '37'},\n",
       " {'user_id': '1', 'movie_id': '38'},\n",
       " {'user_id': '1', 'movie_id': '39'},\n",
       " {'user_id': '1', 'movie_id': '40'},\n",
       " {'user_id': '1', 'movie_id': '41'},\n",
       " {'user_id': '1', 'movie_id': '42'},\n",
       " {'user_id': '1', 'movie_id': '43'},\n",
       " {'user_id': '1', 'movie_id': '44'},\n",
       " {'user_id': '1', 'movie_id': '45'},\n",
       " {'user_id': '1', 'movie_id': '46'},\n",
       " {'user_id': '1', 'movie_id': '47'},\n",
       " {'user_id': '1', 'movie_id': '48'},\n",
       " {'user_id': '1', 'movie_id': '49'},\n",
       " {'user_id': '1', 'movie_id': '50'},\n",
       " {'user_id': '1', 'movie_id': '51'},\n",
       " {'user_id': '1', 'movie_id': '52'},\n",
       " {'user_id': '1', 'movie_id': '53'},\n",
       " {'user_id': '1', 'movie_id': '54'},\n",
       " {'user_id': '1', 'movie_id': '55'},\n",
       " {'user_id': '1', 'movie_id': '56'},\n",
       " {'user_id': '1', 'movie_id': '57'},\n",
       " {'user_id': '1', 'movie_id': '58'},\n",
       " {'user_id': '1', 'movie_id': '59'},\n",
       " {'user_id': '1', 'movie_id': '60'},\n",
       " {'user_id': '1', 'movie_id': '62'},\n",
       " {'user_id': '1', 'movie_id': '63'},\n",
       " {'user_id': '1', 'movie_id': '64'},\n",
       " {'user_id': '1', 'movie_id': '65'},\n",
       " {'user_id': '1', 'movie_id': '66'},\n",
       " {'user_id': '1', 'movie_id': '67'},\n",
       " {'user_id': '1', 'movie_id': '68'},\n",
       " {'user_id': '1', 'movie_id': '69'},\n",
       " {'user_id': '1', 'movie_id': '70'},\n",
       " {'user_id': '1', 'movie_id': '71'},\n",
       " {'user_id': '1', 'movie_id': '72'},\n",
       " {'user_id': '1', 'movie_id': '73'},\n",
       " {'user_id': '1', 'movie_id': '74'},\n",
       " {'user_id': '1', 'movie_id': '75'},\n",
       " {'user_id': '1', 'movie_id': '76'},\n",
       " {'user_id': '1', 'movie_id': '77'},\n",
       " {'user_id': '1', 'movie_id': '78'},\n",
       " {'user_id': '1', 'movie_id': '79'},\n",
       " {'user_id': '1', 'movie_id': '80'},\n",
       " {'user_id': '1', 'movie_id': '81'},\n",
       " {'user_id': '1', 'movie_id': '82'},\n",
       " {'user_id': '1', 'movie_id': '83'},\n",
       " {'user_id': '1', 'movie_id': '84'},\n",
       " {'user_id': '1', 'movie_id': '85'},\n",
       " {'user_id': '1', 'movie_id': '86'},\n",
       " {'user_id': '1', 'movie_id': '87'},\n",
       " {'user_id': '1', 'movie_id': '88'},\n",
       " {'user_id': '1', 'movie_id': '89'},\n",
       " {'user_id': '1', 'movie_id': '90'},\n",
       " {'user_id': '1', 'movie_id': '91'},\n",
       " {'user_id': '1', 'movie_id': '92'},\n",
       " {'user_id': '1', 'movie_id': '93'},\n",
       " {'user_id': '1', 'movie_id': '94'},\n",
       " {'user_id': '1', 'movie_id': '95'},\n",
       " {'user_id': '1', 'movie_id': '96'},\n",
       " {'user_id': '1', 'movie_id': '97'},\n",
       " {'user_id': '1', 'movie_id': '98'},\n",
       " {'user_id': '1', 'movie_id': '99'},\n",
       " {'user_id': '1', 'movie_id': '100'},\n",
       " {'user_id': '1', 'movie_id': '101'},\n",
       " {'user_id': '1', 'movie_id': '102'},\n",
       " {'user_id': '1', 'movie_id': '103'},\n",
       " {'user_id': '1', 'movie_id': '104'},\n",
       " {'user_id': '1', 'movie_id': '105'},\n",
       " {'user_id': '1', 'movie_id': '106'},\n",
       " {'user_id': '1', 'movie_id': '107'},\n",
       " {'user_id': '1', 'movie_id': '108'},\n",
       " {'user_id': '1', 'movie_id': '109'},\n",
       " {'user_id': '1', 'movie_id': '110'},\n",
       " {'user_id': '1', 'movie_id': '111'},\n",
       " {'user_id': '1', 'movie_id': '112'},\n",
       " {'user_id': '1', 'movie_id': '113'},\n",
       " {'user_id': '1', 'movie_id': '114'},\n",
       " {'user_id': '1', 'movie_id': '115'},\n",
       " {'user_id': '1', 'movie_id': '116'},\n",
       " {'user_id': '1', 'movie_id': '118'},\n",
       " {'user_id': '1', 'movie_id': '119'},\n",
       " {'user_id': '1', 'movie_id': '120'},\n",
       " {'user_id': '1', 'movie_id': '121'},\n",
       " {'user_id': '1', 'movie_id': '122'},\n",
       " {'user_id': '1', 'movie_id': '123'},\n",
       " {'user_id': '1', 'movie_id': '124'},\n",
       " {'user_id': '1', 'movie_id': '125'},\n",
       " {'user_id': '1', 'movie_id': '126'},\n",
       " {'user_id': '1', 'movie_id': '127'},\n",
       " {'user_id': '1', 'movie_id': '128'},\n",
       " {'user_id': '1', 'movie_id': '129'},\n",
       " {'user_id': '1', 'movie_id': '130'},\n",
       " {'user_id': '1', 'movie_id': '131'},\n",
       " {'user_id': '1', 'movie_id': '132'},\n",
       " {'user_id': '1', 'movie_id': '133'},\n",
       " {'user_id': '1', 'movie_id': '134'},\n",
       " {'user_id': '1', 'movie_id': '135'},\n",
       " {'user_id': '1', 'movie_id': '136'},\n",
       " {'user_id': '1', 'movie_id': '137'},\n",
       " {'user_id': '1', 'movie_id': '138'},\n",
       " {'user_id': '1', 'movie_id': '139'},\n",
       " {'user_id': '1', 'movie_id': '140'},\n",
       " {'user_id': '1', 'movie_id': '141'},\n",
       " {'user_id': '1', 'movie_id': '142'},\n",
       " {'user_id': '1', 'movie_id': '143'},\n",
       " {'user_id': '1', 'movie_id': '144'},\n",
       " {'user_id': '1', 'movie_id': '145'},\n",
       " {'user_id': '1', 'movie_id': '146'},\n",
       " {'user_id': '1', 'movie_id': '147'},\n",
       " {'user_id': '1', 'movie_id': '148'},\n",
       " {'user_id': '1', 'movie_id': '149'},\n",
       " {'user_id': '1', 'movie_id': '150'},\n",
       " {'user_id': '1', 'movie_id': '151'},\n",
       " {'user_id': '1', 'movie_id': '152'},\n",
       " {'user_id': '1', 'movie_id': '153'},\n",
       " {'user_id': '1', 'movie_id': '154'},\n",
       " {'user_id': '1', 'movie_id': '156'},\n",
       " {'user_id': '1', 'movie_id': '157'},\n",
       " {'user_id': '1', 'movie_id': '158'},\n",
       " {'user_id': '1', 'movie_id': '159'},\n",
       " {'user_id': '1', 'movie_id': '161'},\n",
       " {'user_id': '1', 'movie_id': '162'},\n",
       " {'user_id': '1', 'movie_id': '163'},\n",
       " {'user_id': '1', 'movie_id': '164'},\n",
       " {'user_id': '1', 'movie_id': '165'},\n",
       " {'user_id': '1', 'movie_id': '166'},\n",
       " {'user_id': '1', 'movie_id': '167'},\n",
       " {'user_id': '1', 'movie_id': '168'},\n",
       " {'user_id': '1', 'movie_id': '169'},\n",
       " {'user_id': '1', 'movie_id': '170'},\n",
       " {'user_id': '1', 'movie_id': '172'},\n",
       " {'user_id': '1', 'movie_id': '173'},\n",
       " {'user_id': '1', 'movie_id': '174'},\n",
       " {'user_id': '1', 'movie_id': '175'},\n",
       " {'user_id': '1', 'movie_id': '176'},\n",
       " {'user_id': '1', 'movie_id': '177'},\n",
       " {'user_id': '1', 'movie_id': '178'},\n",
       " {'user_id': '1', 'movie_id': '179'},\n",
       " {'user_id': '1', 'movie_id': '180'},\n",
       " {'user_id': '1', 'movie_id': '181'},\n",
       " {'user_id': '1', 'movie_id': '182'},\n",
       " {'user_id': '1', 'movie_id': '183'},\n",
       " {'user_id': '1', 'movie_id': '184'},\n",
       " {'user_id': '1', 'movie_id': '185'},\n",
       " {'user_id': '1', 'movie_id': '186'},\n",
       " {'user_id': '1', 'movie_id': '187'},\n",
       " {'user_id': '1', 'movie_id': '188'},\n",
       " {'user_id': '1', 'movie_id': '190'},\n",
       " {'user_id': '1', 'movie_id': '191'},\n",
       " {'user_id': '1', 'movie_id': '192'},\n",
       " {'user_id': '1', 'movie_id': '193'},\n",
       " {'user_id': '1', 'movie_id': '194'},\n",
       " {'user_id': '1', 'movie_id': '195'},\n",
       " {'user_id': '1', 'movie_id': '196'},\n",
       " {'user_id': '1', 'movie_id': '197'},\n",
       " {'user_id': '1', 'movie_id': '198'},\n",
       " {'user_id': '1', 'movie_id': '199'},\n",
       " {'user_id': '1', 'movie_id': '200'},\n",
       " {'user_id': '1', 'movie_id': '201'},\n",
       " {'user_id': '1', 'movie_id': '203'},\n",
       " {'user_id': '1', 'movie_id': '204'},\n",
       " {'user_id': '1', 'movie_id': '205'},\n",
       " {'user_id': '1', 'movie_id': '206'},\n",
       " {'user_id': '1', 'movie_id': '207'},\n",
       " {'user_id': '1', 'movie_id': '208'},\n",
       " {'user_id': '1', 'movie_id': '209'},\n",
       " {'user_id': '1', 'movie_id': '210'},\n",
       " {'user_id': '1', 'movie_id': '211'},\n",
       " {'user_id': '1', 'movie_id': '212'},\n",
       " {'user_id': '1', 'movie_id': '213'},\n",
       " {'user_id': '1', 'movie_id': '214'},\n",
       " {'user_id': '1', 'movie_id': '215'},\n",
       " {'user_id': '1', 'movie_id': '216'},\n",
       " {'user_id': '1', 'movie_id': '217'},\n",
       " {'user_id': '1', 'movie_id': '218'},\n",
       " {'user_id': '1', 'movie_id': '219'},\n",
       " {'user_id': '1', 'movie_id': '220'},\n",
       " {'user_id': '1', 'movie_id': '221'},\n",
       " {'user_id': '1', 'movie_id': '222'},\n",
       " {'user_id': '1', 'movie_id': '223'},\n",
       " {'user_id': '1', 'movie_id': '224'},\n",
       " {'user_id': '1', 'movie_id': '225'},\n",
       " {'user_id': '1', 'movie_id': '226'},\n",
       " {'user_id': '1', 'movie_id': '227'},\n",
       " {'user_id': '1', 'movie_id': '228'},\n",
       " {'user_id': '1', 'movie_id': '229'},\n",
       " {'user_id': '1', 'movie_id': '230'},\n",
       " {'user_id': '1', 'movie_id': '231'},\n",
       " {'user_id': '1', 'movie_id': '232'},\n",
       " {'user_id': '1', 'movie_id': '233'},\n",
       " {'user_id': '1', 'movie_id': '234'},\n",
       " {'user_id': '1', 'movie_id': '235'},\n",
       " {'user_id': '1', 'movie_id': '236'},\n",
       " {'user_id': '1', 'movie_id': '237'},\n",
       " {'user_id': '1', 'movie_id': '238'},\n",
       " {'user_id': '1', 'movie_id': '239'},\n",
       " {'user_id': '1', 'movie_id': '240'},\n",
       " {'user_id': '1', 'movie_id': '241'},\n",
       " {'user_id': '1', 'movie_id': '242'},\n",
       " {'user_id': '1', 'movie_id': '243'},\n",
       " {'user_id': '1', 'movie_id': '244'},\n",
       " {'user_id': '1', 'movie_id': '245'},\n",
       " {'user_id': '1', 'movie_id': '246'},\n",
       " {'user_id': '1', 'movie_id': '247'},\n",
       " {'user_id': '1', 'movie_id': '248'},\n",
       " {'user_id': '1', 'movie_id': '249'},\n",
       " {'user_id': '1', 'movie_id': '250'},\n",
       " {'user_id': '1', 'movie_id': '251'},\n",
       " {'user_id': '1', 'movie_id': '252'},\n",
       " {'user_id': '1', 'movie_id': '253'},\n",
       " {'user_id': '1', 'movie_id': '254'},\n",
       " {'user_id': '1', 'movie_id': '255'},\n",
       " {'user_id': '1', 'movie_id': '256'},\n",
       " {'user_id': '1', 'movie_id': '257'},\n",
       " {'user_id': '1', 'movie_id': '258'},\n",
       " {'user_id': '1', 'movie_id': '259'},\n",
       " {'user_id': '1', 'movie_id': '260'},\n",
       " {'user_id': '1', 'movie_id': '261'},\n",
       " {'user_id': '1', 'movie_id': '262'},\n",
       " {'user_id': '1', 'movie_id': '263'},\n",
       " {'user_id': '1', 'movie_id': '264'},\n",
       " {'user_id': '1', 'movie_id': '266'},\n",
       " {'user_id': '1', 'movie_id': '267'},\n",
       " {'user_id': '1', 'movie_id': '268'},\n",
       " {'user_id': '1', 'movie_id': '269'},\n",
       " {'user_id': '1', 'movie_id': '270'},\n",
       " {'user_id': '1', 'movie_id': '271'},\n",
       " {'user_id': '1', 'movie_id': '272'},\n",
       " {'user_id': '2', 'movie_id': '1'},\n",
       " {'user_id': '2', 'movie_id': '10'},\n",
       " {'user_id': '2', 'movie_id': '14'},\n",
       " {'user_id': '2', 'movie_id': '19'},\n",
       " {'user_id': '2', 'movie_id': '25'},\n",
       " {'user_id': '2', 'movie_id': '100'},\n",
       " {'user_id': '2', 'movie_id': '111'},\n",
       " {'user_id': '2', 'movie_id': '127'},\n",
       " {'user_id': '2', 'movie_id': '237'},\n",
       " {'user_id': '2', 'movie_id': '242'},\n",
       " {'user_id': '2', 'movie_id': '255'},\n",
       " {'user_id': '2', 'movie_id': '257'},\n",
       " {'user_id': '2', 'movie_id': '258'},\n",
       " {'user_id': '2', 'movie_id': '269'},\n",
       " {'user_id': '2', 'movie_id': '272'},\n",
       " {'user_id': '2', 'movie_id': '273'},\n",
       " {'user_id': '2', 'movie_id': '274'},\n",
       " {'user_id': '2', 'movie_id': '275'},\n",
       " {'user_id': '2', 'movie_id': '276'},\n",
       " {'user_id': '2', 'movie_id': '277'},\n",
       " {'user_id': '2', 'movie_id': '278'},\n",
       " {'user_id': '2', 'movie_id': '279'},\n",
       " {'user_id': '2', 'movie_id': '282'},\n",
       " {'user_id': '2', 'movie_id': '283'},\n",
       " {'user_id': '2', 'movie_id': '284'},\n",
       " {'user_id': '2', 'movie_id': '285'},\n",
       " {'user_id': '2', 'movie_id': '286'},\n",
       " {'user_id': '2', 'movie_id': '287'},\n",
       " {'user_id': '2', 'movie_id': '288'},\n",
       " {'user_id': '2', 'movie_id': '289'},\n",
       " {'user_id': '2', 'movie_id': '291'},\n",
       " {'user_id': '2', 'movie_id': '293'},\n",
       " {'user_id': '2', 'movie_id': '294'},\n",
       " {'user_id': '2', 'movie_id': '295'},\n",
       " {'user_id': '2', 'movie_id': '296'},\n",
       " {'user_id': '2', 'movie_id': '298'},\n",
       " {'user_id': '2', 'movie_id': '299'},\n",
       " {'user_id': '2', 'movie_id': '300'},\n",
       " {'user_id': '2', 'movie_id': '301'},\n",
       " {'user_id': '2', 'movie_id': '302'},\n",
       " {'user_id': '2', 'movie_id': '303'},\n",
       " {'user_id': '2', 'movie_id': '304'},\n",
       " {'user_id': '2', 'movie_id': '305'},\n",
       " {'user_id': '2', 'movie_id': '306'},\n",
       " {'user_id': '2', 'movie_id': '307'},\n",
       " {'user_id': '2', 'movie_id': '308'},\n",
       " {'user_id': '2', 'movie_id': '309'},\n",
       " {'user_id': '2', 'movie_id': '310'},\n",
       " {'user_id': '2', 'movie_id': '311'},\n",
       " {'user_id': '2', 'movie_id': '313'},\n",
       " {'user_id': '2', 'movie_id': '315'},\n",
       " {'user_id': '2', 'movie_id': '316'},\n",
       " {'user_id': '3', 'movie_id': '181'},\n",
       " {'user_id': '3', 'movie_id': '258'},\n",
       " {'user_id': '3', 'movie_id': '260'},\n",
       " {'user_id': '3', 'movie_id': '264'},\n",
       " {'user_id': '3', 'movie_id': '268'},\n",
       " {'user_id': '3', 'movie_id': '271'},\n",
       " {'user_id': '3', 'movie_id': '272'},\n",
       " {'user_id': '3', 'movie_id': '288'},\n",
       " {'user_id': '3', 'movie_id': '299'},\n",
       " {'user_id': '3', 'movie_id': '300'},\n",
       " {'user_id': '3', 'movie_id': '302'},\n",
       " {'user_id': '3', 'movie_id': '303'},\n",
       " {'user_id': '3', 'movie_id': '307'},\n",
       " {'user_id': '3', 'movie_id': '317'},\n",
       " {'user_id': '3', 'movie_id': '318'},\n",
       " {'user_id': '3', 'movie_id': '319'},\n",
       " {'user_id': '3', 'movie_id': '320'},\n",
       " {'user_id': '3', 'movie_id': '321'},\n",
       " {'user_id': '3', 'movie_id': '322'},\n",
       " {'user_id': '3', 'movie_id': '324'},\n",
       " {'user_id': '3', 'movie_id': '325'},\n",
       " {'user_id': '3', 'movie_id': '326'},\n",
       " {'user_id': '3', 'movie_id': '327'},\n",
       " {'user_id': '3', 'movie_id': '329'},\n",
       " {'user_id': '3', 'movie_id': '330'},\n",
       " {'user_id': '3', 'movie_id': '333'},\n",
       " {'user_id': '3', 'movie_id': '336'},\n",
       " {'user_id': '3', 'movie_id': '338'},\n",
       " {'user_id': '3', 'movie_id': '339'},\n",
       " {'user_id': '3', 'movie_id': '340'},\n",
       " {'user_id': '3', 'movie_id': '341'},\n",
       " {'user_id': '3', 'movie_id': '342'},\n",
       " {'user_id': '3', 'movie_id': '344'},\n",
       " {'user_id': '3', 'movie_id': '345'},\n",
       " {'user_id': '3', 'movie_id': '346'},\n",
       " {'user_id': '3', 'movie_id': '347'},\n",
       " {'user_id': '3', 'movie_id': '348'},\n",
       " {'user_id': '3', 'movie_id': '349'},\n",
       " {'user_id': '3', 'movie_id': '350'},\n",
       " {'user_id': '3', 'movie_id': '351'},\n",
       " {'user_id': '3', 'movie_id': '352'},\n",
       " {'user_id': '3', 'movie_id': '353'},\n",
       " {'user_id': '3', 'movie_id': '354'},\n",
       " {'user_id': '3', 'movie_id': '355'},\n",
       " {'user_id': '4', 'movie_id': '11'},\n",
       " {'user_id': '4', 'movie_id': '210'},\n",
       " {'user_id': '4', 'movie_id': '258'},\n",
       " {'user_id': '4', 'movie_id': '271'},\n",
       " {'user_id': '4', 'movie_id': '300'},\n",
       " {'user_id': '4', 'movie_id': '301'},\n",
       " {'user_id': '4', 'movie_id': '324'},\n",
       " {'user_id': '4', 'movie_id': '327'},\n",
       " {'user_id': '4', 'movie_id': '328'},\n",
       " {'user_id': '4', 'movie_id': '329'},\n",
       " {'user_id': '4', 'movie_id': '358'},\n",
       " {'user_id': '4', 'movie_id': '359'},\n",
       " {'user_id': '4', 'movie_id': '360'},\n",
       " {'user_id': '4', 'movie_id': '362'},\n",
       " {'user_id': '5', 'movie_id': '21'},\n",
       " {'user_id': '5', 'movie_id': '24'},\n",
       " {'user_id': '5', 'movie_id': '25'},\n",
       " {'user_id': '5', 'movie_id': '29'},\n",
       " {'user_id': '5', 'movie_id': '40'},\n",
       " {'user_id': '5', 'movie_id': '42'},\n",
       " {'user_id': '5', 'movie_id': '50'},\n",
       " {'user_id': '5', 'movie_id': '62'},\n",
       " {'user_id': '5', 'movie_id': '63'},\n",
       " {'user_id': '5', 'movie_id': '66'},\n",
       " {'user_id': '5', 'movie_id': '69'},\n",
       " {'user_id': '5', 'movie_id': '70'},\n",
       " {'user_id': '5', 'movie_id': '79'},\n",
       " {'user_id': '5', 'movie_id': '80'},\n",
       " {'user_id': '5', 'movie_id': '89'},\n",
       " {'user_id': '5', 'movie_id': '90'},\n",
       " {'user_id': '5', 'movie_id': '94'},\n",
       " {'user_id': '5', 'movie_id': '95'},\n",
       " {'user_id': '5', 'movie_id': '99'},\n",
       " {'user_id': '5', 'movie_id': '100'},\n",
       " {'user_id': '5', 'movie_id': '101'},\n",
       " {'user_id': '5', 'movie_id': '102'},\n",
       " {'user_id': '5', 'movie_id': '105'},\n",
       " {'user_id': '5', 'movie_id': '109'},\n",
       " {'user_id': '5', 'movie_id': '121'},\n",
       " {'user_id': '5', 'movie_id': '135'},\n",
       " {'user_id': '5', 'movie_id': '139'},\n",
       " {'user_id': '5', 'movie_id': '143'},\n",
       " {'user_id': '5', 'movie_id': '144'},\n",
       " {'user_id': '5', 'movie_id': '145'},\n",
       " {'user_id': '5', 'movie_id': '151'},\n",
       " {'user_id': '5', 'movie_id': '153'},\n",
       " {'user_id': '5', 'movie_id': '154'},\n",
       " {'user_id': '5', 'movie_id': '162'},\n",
       " {'user_id': '5', 'movie_id': '163'},\n",
       " {'user_id': '5', 'movie_id': '167'},\n",
       " {'user_id': '5', 'movie_id': '168'},\n",
       " {'user_id': '5', 'movie_id': '169'},\n",
       " {'user_id': '5', 'movie_id': '172'},\n",
       " {'user_id': '5', 'movie_id': '173'},\n",
       " {'user_id': '5', 'movie_id': '174'},\n",
       " {'user_id': '5', 'movie_id': '176'},\n",
       " {'user_id': '5', 'movie_id': '181'},\n",
       " {'user_id': '5', 'movie_id': '183'},\n",
       " {'user_id': '5', 'movie_id': '185'},\n",
       " {'user_id': '5', 'movie_id': '186'},\n",
       " {'user_id': '5', 'movie_id': '189'},\n",
       " {'user_id': '5', 'movie_id': '194'},\n",
       " {'user_id': '5', 'movie_id': '200'},\n",
       " {'user_id': '5', 'movie_id': '204'},\n",
       " {'user_id': '5', 'movie_id': '208'},\n",
       " {'user_id': '5', 'movie_id': '209'},\n",
       " {'user_id': '5', 'movie_id': '210'},\n",
       " {'user_id': '5', 'movie_id': '211'},\n",
       " {'user_id': '5', 'movie_id': '214'},\n",
       " {'user_id': '5', 'movie_id': '216'},\n",
       " {'user_id': '5', 'movie_id': '219'},\n",
       " {'user_id': '5', 'movie_id': '222'},\n",
       " {'user_id': '5', 'movie_id': '226'},\n",
       " {'user_id': '5', 'movie_id': '227'},\n",
       " {'user_id': '5', 'movie_id': '228'},\n",
       " {'user_id': '5', 'movie_id': '229'},\n",
       " {'user_id': '5', 'movie_id': '230'},\n",
       " {'user_id': '5', 'movie_id': '231'},\n",
       " {'user_id': '5', 'movie_id': '233'},\n",
       " {'user_id': '5', 'movie_id': '234'},\n",
       " {'user_id': '5', 'movie_id': '235'},\n",
       " {'user_id': '5', 'movie_id': '239'},\n",
       " {'user_id': '5', 'movie_id': '241'},\n",
       " {'user_id': '5', 'movie_id': '243'},\n",
       " {'user_id': '5', 'movie_id': '250'},\n",
       " {'user_id': '5', 'movie_id': '257'},\n",
       " {'user_id': '5', 'movie_id': '259'},\n",
       " {'user_id': '5', 'movie_id': '267'},\n",
       " {'user_id': '5', 'movie_id': '364'},\n",
       " {'user_id': '5', 'movie_id': '365'},\n",
       " {'user_id': '5', 'movie_id': '366'},\n",
       " {'user_id': '5', 'movie_id': '367'},\n",
       " {'user_id': '5', 'movie_id': '368'},\n",
       " {'user_id': '5', 'movie_id': '369'},\n",
       " {'user_id': '5', 'movie_id': '370'},\n",
       " {'user_id': '5', 'movie_id': '371'},\n",
       " {'user_id': '5', 'movie_id': '372'},\n",
       " {'user_id': '5', 'movie_id': '373'},\n",
       " {'user_id': '5', 'movie_id': '374'},\n",
       " {'user_id': '5', 'movie_id': '375'},\n",
       " {'user_id': '5', 'movie_id': '376'},\n",
       " {'user_id': '5', 'movie_id': '377'},\n",
       " {'user_id': '5', 'movie_id': '378'},\n",
       " {'user_id': '5', 'movie_id': '379'},\n",
       " {'user_id': '5', 'movie_id': '380'},\n",
       " {'user_id': '5', 'movie_id': '381'},\n",
       " {'user_id': '5', 'movie_id': '382'},\n",
       " {'user_id': '5', 'movie_id': '383'},\n",
       " {'user_id': '5', 'movie_id': '384'},\n",
       " {'user_id': '5', 'movie_id': '385'},\n",
       " {'user_id': '5', 'movie_id': '386'},\n",
       " {'user_id': '5', 'movie_id': '387'},\n",
       " {'user_id': '5', 'movie_id': '388'},\n",
       " {'user_id': '5', 'movie_id': '389'},\n",
       " {'user_id': '5', 'movie_id': '390'},\n",
       " {'user_id': '5', 'movie_id': '391'},\n",
       " {'user_id': '5', 'movie_id': '392'},\n",
       " {'user_id': '5', 'movie_id': '393'},\n",
       " {'user_id': '5', 'movie_id': '394'},\n",
       " {'user_id': '5', 'movie_id': '395'},\n",
       " {'user_id': '5', 'movie_id': '396'},\n",
       " {'user_id': '5', 'movie_id': '397'},\n",
       " {'user_id': '5', 'movie_id': '398'},\n",
       " {'user_id': '5', 'movie_id': '399'},\n",
       " {'user_id': '5', 'movie_id': '400'},\n",
       " {'user_id': '5', 'movie_id': '401'},\n",
       " {'user_id': '5', 'movie_id': '402'},\n",
       " {'user_id': '5', 'movie_id': '403'},\n",
       " {'user_id': '5', 'movie_id': '404'},\n",
       " {'user_id': '5', 'movie_id': '405'},\n",
       " {'user_id': '5', 'movie_id': '406'},\n",
       " {'user_id': '5', 'movie_id': '407'},\n",
       " {'user_id': '5', 'movie_id': '408'},\n",
       " {'user_id': '5', 'movie_id': '409'},\n",
       " {'user_id': '5', 'movie_id': '410'},\n",
       " {'user_id': '5', 'movie_id': '411'},\n",
       " {'user_id': '5', 'movie_id': '412'},\n",
       " {'user_id': '5', 'movie_id': '413'},\n",
       " {'user_id': '5', 'movie_id': '414'},\n",
       " {'user_id': '5', 'movie_id': '415'},\n",
       " {'user_id': '5', 'movie_id': '416'},\n",
       " {'user_id': '5', 'movie_id': '417'},\n",
       " {'user_id': '5', 'movie_id': '418'},\n",
       " {'user_id': '5', 'movie_id': '419'},\n",
       " {'user_id': '5', 'movie_id': '420'},\n",
       " {'user_id': '5', 'movie_id': '421'},\n",
       " {'user_id': '5', 'movie_id': '422'},\n",
       " {'user_id': '5', 'movie_id': '423'},\n",
       " {'user_id': '5', 'movie_id': '425'},\n",
       " {'user_id': '5', 'movie_id': '426'},\n",
       " {'user_id': '5', 'movie_id': '427'},\n",
       " {'user_id': '5', 'movie_id': '428'},\n",
       " {'user_id': '5', 'movie_id': '429'},\n",
       " {'user_id': '5', 'movie_id': '430'},\n",
       " {'user_id': '5', 'movie_id': '431'},\n",
       " {'user_id': '5', 'movie_id': '432'},\n",
       " {'user_id': '5', 'movie_id': '433'},\n",
       " {'user_id': '5', 'movie_id': '434'},\n",
       " {'user_id': '5', 'movie_id': '435'},\n",
       " {'user_id': '5', 'movie_id': '436'},\n",
       " {'user_id': '5', 'movie_id': '437'},\n",
       " {'user_id': '5', 'movie_id': '438'},\n",
       " {'user_id': '5', 'movie_id': '440'},\n",
       " {'user_id': '5', 'movie_id': '441'},\n",
       " {'user_id': '5', 'movie_id': '442'},\n",
       " {'user_id': '5', 'movie_id': '443'},\n",
       " {'user_id': '5', 'movie_id': '444'},\n",
       " {'user_id': '5', 'movie_id': '445'},\n",
       " {'user_id': '5', 'movie_id': '446'},\n",
       " {'user_id': '5', 'movie_id': '447'},\n",
       " {'user_id': '5', 'movie_id': '448'},\n",
       " {'user_id': '5', 'movie_id': '449'},\n",
       " {'user_id': '5', 'movie_id': '450'},\n",
       " {'user_id': '5', 'movie_id': '451'},\n",
       " {'user_id': '5', 'movie_id': '452'},\n",
       " {'user_id': '5', 'movie_id': '453'},\n",
       " {'user_id': '5', 'movie_id': '455'},\n",
       " {'user_id': '5', 'movie_id': '456'},\n",
       " {'user_id': '5', 'movie_id': '457'},\n",
       " {'user_id': '6', 'movie_id': '1'},\n",
       " {'user_id': '6', 'movie_id': '7'},\n",
       " {'user_id': '6', 'movie_id': '8'},\n",
       " {'user_id': '6', 'movie_id': '9'},\n",
       " {'user_id': '6', 'movie_id': '12'},\n",
       " {'user_id': '6', 'movie_id': '13'},\n",
       " {'user_id': '6', 'movie_id': '15'},\n",
       " {'user_id': '6', 'movie_id': '19'},\n",
       " {'user_id': '6', 'movie_id': '21'},\n",
       " {'user_id': '6', 'movie_id': '22'},\n",
       " {'user_id': '6', 'movie_id': '28'},\n",
       " {'user_id': '6', 'movie_id': '32'},\n",
       " {'user_id': '6', 'movie_id': '47'},\n",
       " {'user_id': '6', 'movie_id': '50'},\n",
       " {'user_id': '6', 'movie_id': '56'},\n",
       " {'user_id': '6', 'movie_id': '59'},\n",
       " {'user_id': '6', 'movie_id': '64'},\n",
       " {'user_id': '6', 'movie_id': '70'},\n",
       " {'user_id': '6', 'movie_id': '71'},\n",
       " {'user_id': '6', 'movie_id': '79'},\n",
       " {'user_id': '6', 'movie_id': '81'},\n",
       " {'user_id': '6', 'movie_id': '87'},\n",
       " {'user_id': '6', 'movie_id': '89'},\n",
       " {'user_id': '6', 'movie_id': '95'},\n",
       " {'user_id': '6', 'movie_id': '100'},\n",
       " {'user_id': '6', 'movie_id': '111'},\n",
       " {'user_id': '6', 'movie_id': '117'},\n",
       " {'user_id': '6', 'movie_id': '124'},\n",
       " {'user_id': '6', 'movie_id': '125'},\n",
       " {'user_id': '6', 'movie_id': '127'},\n",
       " {'user_id': '6', 'movie_id': '131'},\n",
       " {'user_id': '6', 'movie_id': '132'},\n",
       " {'user_id': '6', 'movie_id': '133'},\n",
       " {'user_id': '6', 'movie_id': '134'},\n",
       " {'user_id': '6', 'movie_id': '135'},\n",
       " {'user_id': '6', 'movie_id': '136'},\n",
       " {'user_id': '6', 'movie_id': '137'},\n",
       " {'user_id': '6', 'movie_id': '143'},\n",
       " {'user_id': '6', 'movie_id': '151'},\n",
       " {'user_id': '6', 'movie_id': '153'},\n",
       " {'user_id': '6', 'movie_id': '154'},\n",
       " {'user_id': '6', 'movie_id': '156'},\n",
       " {'user_id': '6', 'movie_id': '165'},\n",
       " {'user_id': '6', 'movie_id': '166'},\n",
       " {'user_id': '6', 'movie_id': '168'},\n",
       " {'user_id': '6', 'movie_id': '169'},\n",
       " {'user_id': '6', 'movie_id': '170'},\n",
       " {'user_id': '6', 'movie_id': '173'},\n",
       " {'user_id': '6', 'movie_id': '174'},\n",
       " {'user_id': '6', 'movie_id': '175'},\n",
       " {'user_id': '6', 'movie_id': '177'},\n",
       " {'user_id': '6', 'movie_id': '178'},\n",
       " {'user_id': '6', 'movie_id': '180'},\n",
       " {'user_id': '6', 'movie_id': '182'},\n",
       " {'user_id': '6', 'movie_id': '183'},\n",
       " {'user_id': '6', 'movie_id': '185'},\n",
       " {'user_id': '6', 'movie_id': '186'},\n",
       " {'user_id': '6', 'movie_id': '187'},\n",
       " {'user_id': '6', 'movie_id': '188'},\n",
       " {'user_id': '6', 'movie_id': '189'},\n",
       " {'user_id': '6', 'movie_id': '191'},\n",
       " {'user_id': '6', 'movie_id': '192'},\n",
       " {'user_id': '6', 'movie_id': '193'},\n",
       " {'user_id': '6', 'movie_id': '194'},\n",
       " {'user_id': '6', 'movie_id': '195'},\n",
       " {'user_id': '6', 'movie_id': '197'},\n",
       " {'user_id': '6', 'movie_id': '199'},\n",
       " {'user_id': '6', 'movie_id': '200'},\n",
       " {'user_id': '6', 'movie_id': '202'},\n",
       " {'user_id': '6', 'movie_id': '203'},\n",
       " {'user_id': '6', 'movie_id': '204'},\n",
       " {'user_id': '6', 'movie_id': '205'},\n",
       " {'user_id': '6', 'movie_id': '208'},\n",
       " {'user_id': '6', 'movie_id': '209'},\n",
       " {'user_id': '6', 'movie_id': '211'},\n",
       " {'user_id': '6', 'movie_id': '213'},\n",
       " {'user_id': '6', 'movie_id': '216'},\n",
       " {'user_id': '6', 'movie_id': '221'},\n",
       " {'user_id': '6', 'movie_id': '223'},\n",
       " {'user_id': '6', 'movie_id': '237'},\n",
       " {'user_id': '6', 'movie_id': '238'},\n",
       " {'user_id': '6', 'movie_id': '242'},\n",
       " {'user_id': '6', 'movie_id': '246'},\n",
       " {'user_id': '6', 'movie_id': '248'},\n",
       " {'user_id': '6', 'movie_id': '257'},\n",
       " {'user_id': '6', 'movie_id': '259'},\n",
       " {'user_id': '6', 'movie_id': '261'},\n",
       " {'user_id': '6', 'movie_id': '268'},\n",
       " {'user_id': '6', 'movie_id': '269'},\n",
       " {'user_id': '6', 'movie_id': '272'},\n",
       " {'user_id': '6', 'movie_id': '274'},\n",
       " {'user_id': '6', 'movie_id': '275'},\n",
       " {'user_id': '6', 'movie_id': '276'},\n",
       " {'user_id': '6', 'movie_id': '284'},\n",
       " {'user_id': '6', 'movie_id': '285'},\n",
       " {'user_id': '6', 'movie_id': '286'},\n",
       " {'user_id': '6', 'movie_id': '293'},\n",
       " {'user_id': '6', 'movie_id': '294'},\n",
       " {'user_id': '6', 'movie_id': '297'},\n",
       " {'user_id': '6', 'movie_id': '298'},\n",
       " {'user_id': '6', 'movie_id': '302'},\n",
       " {'user_id': '6', 'movie_id': '303'},\n",
       " {'user_id': '6', 'movie_id': '304'},\n",
       " {'user_id': '6', 'movie_id': '306'},\n",
       " {'user_id': '6', 'movie_id': '308'},\n",
       " {'user_id': '6', 'movie_id': '309'},\n",
       " {'user_id': '6', 'movie_id': '310'},\n",
       " {'user_id': '6', 'movie_id': '317'},\n",
       " {'user_id': '6', 'movie_id': '318'},\n",
       " {'user_id': '6', 'movie_id': '321'},\n",
       " {'user_id': '6', 'movie_id': '340'},\n",
       " {'user_id': '6', 'movie_id': '357'},\n",
       " {'user_id': '6', 'movie_id': '367'},\n",
       " {'user_id': '6', 'movie_id': '405'},\n",
       " {'user_id': '6', 'movie_id': '408'},\n",
       " {'user_id': '6', 'movie_id': '410'},\n",
       " {'user_id': '6', 'movie_id': '419'},\n",
       " {'user_id': '6', 'movie_id': '423'},\n",
       " {'user_id': '6', 'movie_id': '425'},\n",
       " {'user_id': '6', 'movie_id': '427'},\n",
       " {'user_id': '6', 'movie_id': '432'},\n",
       " {'user_id': '6', 'movie_id': '435'},\n",
       " {'user_id': '6', 'movie_id': '458'},\n",
       " {'user_id': '6', 'movie_id': '459'},\n",
       " {'user_id': '6', 'movie_id': '460'},\n",
       " {'user_id': '6', 'movie_id': '461'},\n",
       " {'user_id': '6', 'movie_id': '462'},\n",
       " {'user_id': '6', 'movie_id': '464'},\n",
       " {'user_id': '6', 'movie_id': '465'},\n",
       " {'user_id': '6', 'movie_id': '466'},\n",
       " {'user_id': '6', 'movie_id': '467'},\n",
       " {'user_id': '6', 'movie_id': '468'},\n",
       " {'user_id': '6', 'movie_id': '469'},\n",
       " {'user_id': '6', 'movie_id': '470'},\n",
       " {'user_id': '6', 'movie_id': '471'},\n",
       " {'user_id': '6', 'movie_id': '472'},\n",
       " {'user_id': '6', 'movie_id': '473'},\n",
       " {'user_id': '6', 'movie_id': '474'},\n",
       " {'user_id': '6', 'movie_id': '475'},\n",
       " {'user_id': '6', 'movie_id': '476'},\n",
       " {'user_id': '6', 'movie_id': '477'},\n",
       " {'user_id': '6', 'movie_id': '478'},\n",
       " {'user_id': '6', 'movie_id': '479'},\n",
       " {'user_id': '6', 'movie_id': '480'},\n",
       " {'user_id': '6', 'movie_id': '481'},\n",
       " {'user_id': '6', 'movie_id': '482'},\n",
       " {'user_id': '6', 'movie_id': '483'},\n",
       " {'user_id': '6', 'movie_id': '484'},\n",
       " {'user_id': '6', 'movie_id': '485'},\n",
       " {'user_id': '6', 'movie_id': '486'},\n",
       " {'user_id': '6', 'movie_id': '487'},\n",
       " {'user_id': '6', 'movie_id': '488'},\n",
       " {'user_id': '6', 'movie_id': '489'},\n",
       " {'user_id': '6', 'movie_id': '490'},\n",
       " {'user_id': '6', 'movie_id': '491'},\n",
       " {'user_id': '6', 'movie_id': '493'},\n",
       " {'user_id': '6', 'movie_id': '494'},\n",
       " {'user_id': '6', 'movie_id': '495'},\n",
       " {'user_id': '6', 'movie_id': '496'},\n",
       " {'user_id': '6', 'movie_id': '497'},\n",
       " {'user_id': '6', 'movie_id': '498'},\n",
       " {'user_id': '6', 'movie_id': '499'},\n",
       " {'user_id': '6', 'movie_id': '500'},\n",
       " {'user_id': '6', 'movie_id': '501'},\n",
       " {'user_id': '6', 'movie_id': '502'},\n",
       " {'user_id': '6', 'movie_id': '503'},\n",
       " {'user_id': '6', 'movie_id': '504'},\n",
       " {'user_id': '6', 'movie_id': '505'},\n",
       " {'user_id': '6', 'movie_id': '506'},\n",
       " {'user_id': '6', 'movie_id': '507'},\n",
       " {'user_id': '6', 'movie_id': '508'},\n",
       " {'user_id': '6', 'movie_id': '509'},\n",
       " {'user_id': '6', 'movie_id': '510'},\n",
       " {'user_id': '6', 'movie_id': '511'},\n",
       " {'user_id': '6', 'movie_id': '512'},\n",
       " {'user_id': '6', 'movie_id': '513'},\n",
       " {'user_id': '6', 'movie_id': '514'},\n",
       " {'user_id': '6', 'movie_id': '515'},\n",
       " {'user_id': '6', 'movie_id': '516'},\n",
       " {'user_id': '6', 'movie_id': '518'},\n",
       " {'user_id': '6', 'movie_id': '519'},\n",
       " {'user_id': '6', 'movie_id': '520'},\n",
       " {'user_id': '6', 'movie_id': '521'},\n",
       " {'user_id': '6', 'movie_id': '522'},\n",
       " {'user_id': '6', 'movie_id': '523'},\n",
       " {'user_id': '6', 'movie_id': '524'},\n",
       " {'user_id': '6', 'movie_id': '525'},\n",
       " {'user_id': '6', 'movie_id': '526'},\n",
       " {'user_id': '6', 'movie_id': '527'},\n",
       " {'user_id': '6', 'movie_id': '528'},\n",
       " {'user_id': '6', 'movie_id': '529'},\n",
       " {'user_id': '6', 'movie_id': '530'},\n",
       " {'user_id': '6', 'movie_id': '531'},\n",
       " {'user_id': '6', 'movie_id': '532'},\n",
       " {'user_id': '6', 'movie_id': '533'},\n",
       " {'user_id': '6', 'movie_id': '534'},\n",
       " {'user_id': '6', 'movie_id': '535'},\n",
       " {'user_id': '6', 'movie_id': '536'},\n",
       " {'user_id': '6', 'movie_id': '537'},\n",
       " {'user_id': '6', 'movie_id': '538'},\n",
       " {'user_id': '6', 'movie_id': '539'},\n",
       " {'user_id': '7', 'movie_id': '4'},\n",
       " {'user_id': '7', 'movie_id': '7'},\n",
       " {'user_id': '7', 'movie_id': '8'},\n",
       " {'user_id': '7', 'movie_id': '9'},\n",
       " {'user_id': '7', 'movie_id': '10'},\n",
       " {'user_id': '7', 'movie_id': '11'},\n",
       " {'user_id': '7', 'movie_id': '12'},\n",
       " {'user_id': '7', 'movie_id': '22'},\n",
       " {'user_id': '7', 'movie_id': '23'},\n",
       " {'user_id': '7', 'movie_id': '25'},\n",
       " {'user_id': '7', 'movie_id': '27'},\n",
       " {'user_id': '7', 'movie_id': '28'},\n",
       " {'user_id': '7', 'movie_id': '29'},\n",
       " {'user_id': '7', 'movie_id': '31'},\n",
       " {'user_id': '7', 'movie_id': '39'},\n",
       " {'user_id': '7', 'movie_id': '44'},\n",
       " {'user_id': '7', 'movie_id': '47'},\n",
       " {'user_id': '7', 'movie_id': '50'},\n",
       " {'user_id': '7', 'movie_id': '51'},\n",
       " {'user_id': '7', 'movie_id': '52'},\n",
       " {'user_id': '7', 'movie_id': '53'},\n",
       " {'user_id': '7', 'movie_id': '54'},\n",
       " {'user_id': '7', 'movie_id': '56'},\n",
       " {'user_id': '7', 'movie_id': '62'},\n",
       " {'user_id': '7', 'movie_id': '64'},\n",
       " {'user_id': '7', 'movie_id': '68'},\n",
       " {'user_id': '7', 'movie_id': '69'},\n",
       " {'user_id': '7', 'movie_id': '70'},\n",
       " {'user_id': '7', 'movie_id': '71'},\n",
       " {'user_id': '7', 'movie_id': '72'},\n",
       " {'user_id': '7', 'movie_id': '73'},\n",
       " {'user_id': '7', 'movie_id': '77'},\n",
       " {'user_id': '7', 'movie_id': '78'},\n",
       " {'user_id': '7', 'movie_id': '79'},\n",
       " {'user_id': '7', 'movie_id': '80'},\n",
       " {'user_id': '7', 'movie_id': '81'},\n",
       " {'user_id': '7', 'movie_id': '82'},\n",
       " {'user_id': '7', 'movie_id': '86'},\n",
       " {'user_id': '7', 'movie_id': '89'},\n",
       " {'user_id': '7', 'movie_id': '90'},\n",
       " {'user_id': '7', 'movie_id': '91'},\n",
       " {'user_id': '7', 'movie_id': '92'},\n",
       " {'user_id': '7', 'movie_id': '93'},\n",
       " {'user_id': '7', 'movie_id': '96'},\n",
       " {'user_id': '7', 'movie_id': '97'},\n",
       " {'user_id': '7', 'movie_id': '98'},\n",
       " {'user_id': '7', 'movie_id': '99'},\n",
       " {'user_id': '7', 'movie_id': '100'},\n",
       " {'user_id': '7', 'movie_id': '101'},\n",
       " {'user_id': '7', 'movie_id': '106'},\n",
       " {'user_id': '7', 'movie_id': '118'},\n",
       " {'user_id': '7', 'movie_id': '121'},\n",
       " {'user_id': '7', 'movie_id': '125'},\n",
       " {'user_id': '7', 'movie_id': '126'},\n",
       " {'user_id': '7', 'movie_id': '127'},\n",
       " {'user_id': '7', 'movie_id': '131'},\n",
       " {'user_id': '7', 'movie_id': '132'},\n",
       " {'user_id': '7', 'movie_id': '133'},\n",
       " {'user_id': '7', 'movie_id': '134'},\n",
       " {'user_id': '7', 'movie_id': '135'},\n",
       " {'user_id': '7', 'movie_id': '136'},\n",
       " {'user_id': '7', 'movie_id': '139'},\n",
       " {'user_id': '7', 'movie_id': '140'},\n",
       " {'user_id': '7', 'movie_id': '141'},\n",
       " {'user_id': '7', 'movie_id': '142'},\n",
       " {'user_id': '7', 'movie_id': '143'},\n",
       " {'user_id': '7', 'movie_id': '144'},\n",
       " {'user_id': '7', 'movie_id': '145'},\n",
       " {'user_id': '7', 'movie_id': '151'},\n",
       " {'user_id': '7', 'movie_id': '152'},\n",
       " {'user_id': '7', 'movie_id': '153'},\n",
       " {'user_id': '7', 'movie_id': '154'},\n",
       " {'user_id': '7', 'movie_id': '156'},\n",
       " {'user_id': '7', 'movie_id': '157'},\n",
       " {'user_id': '7', 'movie_id': '161'},\n",
       " {'user_id': '7', 'movie_id': '162'},\n",
       " {'user_id': '7', 'movie_id': '164'},\n",
       " {'user_id': '7', 'movie_id': '166'},\n",
       " {'user_id': '7', 'movie_id': '168'},\n",
       " {'user_id': '7', 'movie_id': '171'},\n",
       " {'user_id': '7', 'movie_id': '172'},\n",
       " {'user_id': '7', 'movie_id': '173'},\n",
       " {'user_id': '7', 'movie_id': '174'},\n",
       " {'user_id': '7', 'movie_id': '175'},\n",
       " {'user_id': '7', 'movie_id': '176'},\n",
       " {'user_id': '7', 'movie_id': '177'},\n",
       " {'user_id': '7', 'movie_id': '178'},\n",
       " {'user_id': '7', 'movie_id': '179'},\n",
       " {'user_id': '7', 'movie_id': '180'},\n",
       " {'user_id': '7', 'movie_id': '181'},\n",
       " {'user_id': '7', 'movie_id': '182'},\n",
       " {'user_id': '7', 'movie_id': '183'},\n",
       " {'user_id': '7', 'movie_id': '185'},\n",
       " {'user_id': '7', 'movie_id': '186'},\n",
       " {'user_id': '7', 'movie_id': '187'},\n",
       " {'user_id': '7', 'movie_id': '188'},\n",
       " {'user_id': '7', 'movie_id': '190'},\n",
       " {'user_id': '7', 'movie_id': '191'},\n",
       " {'user_id': '7', 'movie_id': '192'},\n",
       " {'user_id': '7', 'movie_id': '193'},\n",
       " {'user_id': '7', 'movie_id': '194'},\n",
       " {'user_id': '7', 'movie_id': '195'},\n",
       " {'user_id': '7', 'movie_id': '196'},\n",
       " {'user_id': '7', 'movie_id': '197'},\n",
       " {'user_id': '7', 'movie_id': '198'},\n",
       " {'user_id': '7', 'movie_id': '199'},\n",
       " {'user_id': '7', 'movie_id': '200'},\n",
       " {'user_id': '7', 'movie_id': '201'},\n",
       " {'user_id': '7', 'movie_id': '202'},\n",
       " {'user_id': '7', 'movie_id': '203'},\n",
       " {'user_id': '7', 'movie_id': '204'},\n",
       " {'user_id': '7', 'movie_id': '205'},\n",
       " {'user_id': '7', 'movie_id': '207'},\n",
       " {'user_id': '7', 'movie_id': '208'},\n",
       " {'user_id': '7', 'movie_id': '210'},\n",
       " {'user_id': '7', 'movie_id': '211'},\n",
       " {'user_id': '7', 'movie_id': '212'},\n",
       " {'user_id': '7', 'movie_id': '213'},\n",
       " {'user_id': '7', 'movie_id': '214'},\n",
       " {'user_id': '7', 'movie_id': '215'},\n",
       " {'user_id': '7', 'movie_id': '216'},\n",
       " {'user_id': '7', 'movie_id': '217'},\n",
       " {'user_id': '7', 'movie_id': '219'},\n",
       " {'user_id': '7', 'movie_id': '223'},\n",
       " {'user_id': '7', 'movie_id': '226'},\n",
       " {'user_id': '7', 'movie_id': '227'},\n",
       " {'user_id': '7', 'movie_id': '228'},\n",
       " {'user_id': '7', 'movie_id': '229'},\n",
       " {'user_id': '7', 'movie_id': '230'},\n",
       " {'user_id': '7', 'movie_id': '231'},\n",
       " {'user_id': '7', 'movie_id': '232'},\n",
       " {'user_id': '7', 'movie_id': '234'},\n",
       " {'user_id': '7', 'movie_id': '237'},\n",
       " {'user_id': '7', 'movie_id': '238'},\n",
       " {'user_id': '7', 'movie_id': '241'},\n",
       " {'user_id': '7', 'movie_id': '258'},\n",
       " {'user_id': '7', 'movie_id': '259'},\n",
       " {'user_id': '7', 'movie_id': '260'},\n",
       " {'user_id': '7', 'movie_id': '264'},\n",
       " {'user_id': '7', 'movie_id': '265'},\n",
       " {'user_id': '7', 'movie_id': '266'},\n",
       " {'user_id': '7', 'movie_id': '268'},\n",
       " {'user_id': '7', 'movie_id': '269'},\n",
       " {'user_id': '7', 'movie_id': '273'},\n",
       " {'user_id': '7', 'movie_id': '275'},\n",
       " {'user_id': '7', 'movie_id': '281'},\n",
       " {'user_id': '7', 'movie_id': '285'},\n",
       " {'user_id': '7', 'movie_id': '286'},\n",
       " {'user_id': '7', 'movie_id': '288'},\n",
       " {'user_id': '7', 'movie_id': '294'},\n",
       " {'user_id': '7', 'movie_id': '300'},\n",
       " {'user_id': '7', 'movie_id': '307'},\n",
       " {'user_id': '7', 'movie_id': '309'},\n",
       " {'user_id': '7', 'movie_id': '317'},\n",
       " {'user_id': '7', 'movie_id': '318'},\n",
       " {'user_id': '7', 'movie_id': '324'},\n",
       " {'user_id': '7', 'movie_id': '334'},\n",
       " {'user_id': '7', 'movie_id': '341'},\n",
       " {'user_id': '7', 'movie_id': '356'},\n",
       " {'user_id': '7', 'movie_id': '357'},\n",
       " {'user_id': '7', 'movie_id': '365'},\n",
       " {'user_id': '7', 'movie_id': '367'},\n",
       " {'user_id': '7', 'movie_id': '378'},\n",
       " {'user_id': '7', 'movie_id': '379'},\n",
       " {'user_id': '7', 'movie_id': '380'},\n",
       " {'user_id': '7', 'movie_id': '384'},\n",
       " {'user_id': '7', 'movie_id': '385'},\n",
       " {'user_id': '7', 'movie_id': '386'},\n",
       " {'user_id': '7', 'movie_id': '387'},\n",
       " {'user_id': '7', 'movie_id': '389'},\n",
       " {'user_id': '7', 'movie_id': '391'},\n",
       " {'user_id': '7', 'movie_id': '393'},\n",
       " {'user_id': '7', 'movie_id': '396'},\n",
       " {'user_id': '7', 'movie_id': '399'},\n",
       " {'user_id': '7', 'movie_id': '401'},\n",
       " {'user_id': '7', 'movie_id': '402'},\n",
       " {'user_id': '7', 'movie_id': '403'},\n",
       " {'user_id': '7', 'movie_id': '404'},\n",
       " {'user_id': '7', 'movie_id': '405'},\n",
       " {'user_id': '7', 'movie_id': '415'},\n",
       " {'user_id': '7', 'movie_id': '416'},\n",
       " {'user_id': '7', 'movie_id': '417'},\n",
       " {'user_id': '7', 'movie_id': '418'},\n",
       " {'user_id': '7', 'movie_id': '419'},\n",
       " {'user_id': '7', 'movie_id': '420'},\n",
       " {'user_id': '7', 'movie_id': '421'},\n",
       " {'user_id': '7', 'movie_id': '423'},\n",
       " {'user_id': '7', 'movie_id': '427'},\n",
       " {'user_id': '7', 'movie_id': '428'},\n",
       " {'user_id': '7', 'movie_id': '429'},\n",
       " {'user_id': '7', 'movie_id': '431'},\n",
       " {'user_id': '7', 'movie_id': '432'},\n",
       " {'user_id': '7', 'movie_id': '433'},\n",
       " {'user_id': '7', 'movie_id': '434'},\n",
       " {'user_id': '7', 'movie_id': '435'},\n",
       " {'user_id': '7', 'movie_id': '436'},\n",
       " {'user_id': '7', 'movie_id': '440'},\n",
       " {'user_id': '7', 'movie_id': '441'},\n",
       " {'user_id': '7', 'movie_id': '443'},\n",
       " {'user_id': '7', 'movie_id': '444'},\n",
       " {'user_id': '7', 'movie_id': '446'},\n",
       " {'user_id': '7', 'movie_id': '447'},\n",
       " {'user_id': '7', 'movie_id': '448'},\n",
       " {'user_id': '7', 'movie_id': '449'},\n",
       " {'user_id': '7', 'movie_id': '450'},\n",
       " {'user_id': '7', 'movie_id': '451'},\n",
       " {'user_id': '7', 'movie_id': '452'},\n",
       " {'user_id': '7', 'movie_id': '461'},\n",
       " {'user_id': '7', 'movie_id': '463'},\n",
       " {'user_id': '7', 'movie_id': '465'},\n",
       " {'user_id': '7', 'movie_id': '470'},\n",
       " {'user_id': '7', 'movie_id': '471'},\n",
       " {'user_id': '7', 'movie_id': '472'},\n",
       " {'user_id': '7', 'movie_id': '474'},\n",
       " {'user_id': '7', 'movie_id': '480'},\n",
       " {'user_id': '7', 'movie_id': '481'},\n",
       " {'user_id': '7', 'movie_id': '482'},\n",
       " {'user_id': '7', 'movie_id': '483'},\n",
       " {'user_id': '7', 'movie_id': '484'},\n",
       " {'user_id': '7', 'movie_id': '485'},\n",
       " {'user_id': '7', 'movie_id': '487'},\n",
       " {'user_id': '7', 'movie_id': '488'},\n",
       " {'user_id': '7', 'movie_id': '489'},\n",
       " {'user_id': '7', 'movie_id': '491'},\n",
       " {'user_id': '7', 'movie_id': '495'},\n",
       " {'user_id': '7', 'movie_id': '496'},\n",
       " {'user_id': '7', 'movie_id': '498'},\n",
       " {'user_id': '7', 'movie_id': '499'},\n",
       " {'user_id': '7', 'movie_id': '501'},\n",
       " {'user_id': '7', 'movie_id': '502'},\n",
       " {'user_id': '7', 'movie_id': '503'},\n",
       " {'user_id': '7', 'movie_id': '504'},\n",
       " {'user_id': '7', 'movie_id': '505'},\n",
       " {'user_id': '7', 'movie_id': '506'},\n",
       " {'user_id': '7', 'movie_id': '507'},\n",
       " {'user_id': '7', 'movie_id': '509'},\n",
       " {'user_id': '7', 'movie_id': '510'},\n",
       " {'user_id': '7', 'movie_id': '511'},\n",
       " {'user_id': '7', 'movie_id': '513'},\n",
       " {'user_id': '7', 'movie_id': '514'},\n",
       " {'user_id': '7', 'movie_id': '515'},\n",
       " {'user_id': '7', 'movie_id': '519'},\n",
       " {'user_id': '7', 'movie_id': '520'},\n",
       " {'user_id': '7', 'movie_id': '521'},\n",
       " {'user_id': '7', 'movie_id': '523'},\n",
       " {'user_id': '7', 'movie_id': '526'},\n",
       " {'user_id': '7', 'movie_id': '527'},\n",
       " {'user_id': '7', 'movie_id': '528'},\n",
       " {'user_id': '7', 'movie_id': '529'},\n",
       " {'user_id': '7', 'movie_id': '530'},\n",
       " {'user_id': '7', 'movie_id': '537'},\n",
       " {'user_id': '7', 'movie_id': '540'},\n",
       " {'user_id': '7', 'movie_id': '541'},\n",
       " {'user_id': '7', 'movie_id': '542'},\n",
       " {'user_id': '7', 'movie_id': '543'},\n",
       " {'user_id': '7', 'movie_id': '544'},\n",
       " {'user_id': '7', 'movie_id': '545'},\n",
       " {'user_id': '7', 'movie_id': '546'},\n",
       " {'user_id': '7', 'movie_id': '547'},\n",
       " {'user_id': '7', 'movie_id': '548'},\n",
       " {'user_id': '7', 'movie_id': '549'},\n",
       " {'user_id': '7', 'movie_id': '550'},\n",
       " ...]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T11:56:50.111992Z",
     "start_time": "2020-09-25T11:56:49.884344Z"
    }
   },
   "outputs": [],
   "source": [
    "# 下面需要转成one-hot\n",
    "v = DictVectorizer()\n",
    "X_train = v.fit_transform(train_data)\n",
    "X_test = v.transform(test_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T11:58:39.679494Z",
     "start_time": "2020-09-25T11:58:39.675478Z"
    }
   },
   "outputs": [],
   "source": [
    "# 建立FM模型 \n",
    "fm = pylibfm.FM(num_factors=10, num_iter=100, verbose=True, task='regression', initial_learning_rate=0.001, learning_rate_schedule='optimal')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "FM的具体参数函数如下: 这里面重点需要设置的我已标出(详细的可以参考源码)\n",
    "* **num_factors**: 隐向量的维度， 也就是k\n",
    "* **num_iter**: 迭代次数， 由于使用的SGD， 随机梯度下降， 要指明迭代多少个epoch\n",
    "* k0, k1: k0表示是否用偏置（看FM的公式)， k1表示是否要第二项， 就是单个特征的， 这俩默认True\n",
    "* init_stdev: 初始化隐向量时候的方差, 默认0.01\n",
    "* **validation_size**: 验证集的比例， 默认0.01\n",
    "* learning_rate_schedule: 学习率衰减方式， 有constant, optimal, 和invscaling三种方式， 具体公式看源码\n",
    "* **initial_learning_rate**: 初始学习率， 默认0.01\n",
    "* power_t， t0: 逆缩放学习率的指数，最优学习率分母常数， 这两个和上面学习率衰减方式的计算有关\n",
    "* **task**: 分类或者回归任务， 要指明\n",
    "* verbose: 是否打印当前的迭代次数， 训练误差\n",
    "* shuffle_training: 是否在学习之前打乱训练集\n",
    "* seed: 随机种子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T12:17:01.978013Z",
     "start_time": "2020-09-25T12:13:23.791013Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Creating validation dataset of 0.01 of training for adaptive regularization\n",
      "-- Epoch 1\n",
      "Training MSE: 0.59515\n",
      "-- Epoch 2\n",
      "Training MSE: 0.51796\n",
      "-- Epoch 3\n",
      "Training MSE: 0.49021\n",
      "-- Epoch 4\n",
      "Training MSE: 0.47434\n",
      "-- Epoch 5\n",
      "Training MSE: 0.46388\n",
      "-- Epoch 6\n",
      "Training MSE: 0.45643\n",
      "-- Epoch 7\n",
      "Training MSE: 0.45072\n",
      "-- Epoch 8\n",
      "Training MSE: 0.44616\n",
      "-- Epoch 9\n",
      "Training MSE: 0.44245\n",
      "-- Epoch 10\n",
      "Training MSE: 0.43925\n",
      "-- Epoch 11\n",
      "Training MSE: 0.43646\n",
      "-- Epoch 12\n",
      "Training MSE: 0.43414\n",
      "-- Epoch 13\n",
      "Training MSE: 0.43197\n",
      "-- Epoch 14\n",
      "Training MSE: 0.43014\n",
      "-- Epoch 15\n",
      "Training MSE: 0.42832\n",
      "-- Epoch 16\n",
      "Training MSE: 0.42672\n",
      "-- Epoch 17\n",
      "Training MSE: 0.42534\n",
      "-- Epoch 18\n",
      "Training MSE: 0.42394\n",
      "-- Epoch 19\n",
      "Training MSE: 0.42274\n",
      "-- Epoch 20\n",
      "Training MSE: 0.42146\n",
      "-- Epoch 21\n",
      "Training MSE: 0.42029\n",
      "-- Epoch 22\n",
      "Training MSE: 0.41925\n",
      "-- Epoch 23\n",
      "Training MSE: 0.41809\n",
      "-- Epoch 24\n",
      "Training MSE: 0.41714\n",
      "-- Epoch 25\n",
      "Training MSE: 0.41611\n",
      "-- Epoch 26\n",
      "Training MSE: 0.41504\n",
      "-- Epoch 27\n",
      "Training MSE: 0.41400\n",
      "-- Epoch 28\n",
      "Training MSE: 0.41299\n",
      "-- Epoch 29\n",
      "Training MSE: 0.41185\n",
      "-- Epoch 30\n",
      "Training MSE: 0.41090\n",
      "-- Epoch 31\n",
      "Training MSE: 0.40969\n",
      "-- Epoch 32\n",
      "Training MSE: 0.40863\n",
      "-- Epoch 33\n",
      "Training MSE: 0.40745\n",
      "-- Epoch 34\n",
      "Training MSE: 0.40624\n",
      "-- Epoch 35\n",
      "Training MSE: 0.40514\n",
      "-- Epoch 36\n",
      "Training MSE: 0.40382\n",
      "-- Epoch 37\n",
      "Training MSE: 0.40237\n",
      "-- Epoch 38\n",
      "Training MSE: 0.40096\n",
      "-- Epoch 39\n",
      "Training MSE: 0.39958\n",
      "-- Epoch 40\n",
      "Training MSE: 0.39808\n",
      "-- Epoch 41\n",
      "Training MSE: 0.39662\n",
      "-- Epoch 42\n",
      "Training MSE: 0.39496\n",
      "-- Epoch 43\n",
      "Training MSE: 0.39344\n",
      "-- Epoch 44\n",
      "Training MSE: 0.39173\n",
      "-- Epoch 45\n",
      "Training MSE: 0.39005\n",
      "-- Epoch 46\n",
      "Training MSE: 0.38818\n",
      "-- Epoch 47\n",
      "Training MSE: 0.38649\n",
      "-- Epoch 48\n",
      "Training MSE: 0.38474\n",
      "-- Epoch 49\n",
      "Training MSE: 0.38286\n",
      "-- Epoch 50\n",
      "Training MSE: 0.38096\n",
      "-- Epoch 51\n",
      "Training MSE: 0.37907\n",
      "-- Epoch 52\n",
      "Training MSE: 0.37714\n",
      "-- Epoch 53\n",
      "Training MSE: 0.37529\n",
      "-- Epoch 54\n",
      "Training MSE: 0.37333\n",
      "-- Epoch 55\n",
      "Training MSE: 0.37141\n",
      "-- Epoch 56\n",
      "Training MSE: 0.36954\n",
      "-- Epoch 57\n",
      "Training MSE: 0.36766\n",
      "-- Epoch 58\n",
      "Training MSE: 0.36581\n",
      "-- Epoch 59\n",
      "Training MSE: 0.36405\n",
      "-- Epoch 60\n",
      "Training MSE: 0.36223\n",
      "-- Epoch 61\n",
      "Training MSE: 0.36051\n",
      "-- Epoch 62\n",
      "Training MSE: 0.35882\n",
      "-- Epoch 63\n",
      "Training MSE: 0.35706\n",
      "-- Epoch 64\n",
      "Training MSE: 0.35549\n",
      "-- Epoch 65\n",
      "Training MSE: 0.35400\n",
      "-- Epoch 66\n",
      "Training MSE: 0.35244\n",
      "-- Epoch 67\n",
      "Training MSE: 0.35107\n",
      "-- Epoch 68\n",
      "Training MSE: 0.34963\n",
      "-- Epoch 69\n",
      "Training MSE: 0.34816\n",
      "-- Epoch 70\n",
      "Training MSE: 0.34700\n",
      "-- Epoch 71\n",
      "Training MSE: 0.34558\n",
      "-- Epoch 72\n",
      "Training MSE: 0.34440\n",
      "-- Epoch 73\n",
      "Training MSE: 0.34333\n",
      "-- Epoch 74\n",
      "Training MSE: 0.34219\n",
      "-- Epoch 75\n",
      "Training MSE: 0.34118\n",
      "-- Epoch 76\n",
      "Training MSE: 0.34024\n",
      "-- Epoch 77\n",
      "Training MSE: 0.33917\n",
      "-- Epoch 78\n",
      "Training MSE: 0.33827\n",
      "-- Epoch 79\n",
      "Training MSE: 0.33744\n",
      "-- Epoch 80\n",
      "Training MSE: 0.33641\n",
      "-- Epoch 81\n",
      "Training MSE: 0.33576\n",
      "-- Epoch 82\n",
      "Training MSE: 0.33504\n",
      "-- Epoch 83\n",
      "Training MSE: 0.33432\n",
      "-- Epoch 84\n",
      "Training MSE: 0.33362\n",
      "-- Epoch 85\n",
      "Training MSE: 0.33309\n",
      "-- Epoch 86\n",
      "Training MSE: 0.33245\n",
      "-- Epoch 87\n",
      "Training MSE: 0.33188\n",
      "-- Epoch 88\n",
      "Training MSE: 0.33143\n",
      "-- Epoch 89\n",
      "Training MSE: 0.33083\n",
      "-- Epoch 90\n",
      "Training MSE: 0.33047\n",
      "-- Epoch 91\n",
      "Training MSE: 0.32996\n",
      "-- Epoch 92\n",
      "Training MSE: 0.32964\n",
      "-- Epoch 93\n",
      "Training MSE: 0.32925\n",
      "-- Epoch 94\n",
      "Training MSE: 0.32897\n",
      "-- Epoch 95\n",
      "Training MSE: 0.32856\n",
      "-- Epoch 96\n",
      "Training MSE: 0.32832\n",
      "-- Epoch 97\n",
      "Training MSE: 0.32806\n",
      "-- Epoch 98\n",
      "Training MSE: 0.32779\n",
      "-- Epoch 99\n",
      "Training MSE: 0.32748\n",
      "-- Epoch 100\n",
      "Training MSE: 0.32730\n"
     ]
    }
   ],
   "source": [
    "# 模型训练\n",
    "fm.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T12:20:51.176622Z",
     "start_time": "2020-09-25T12:20:51.135679Z"
    }
   },
   "outputs": [],
   "source": [
    "# 评估\n",
    "preds = fm.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T12:20:53.819466Z",
     "start_time": "2020-09-25T12:20:53.815478Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3.82775899, 3.36936358, 3.97046438, ..., 3.1684557 , 2.83862524,\n",
       "       3.30447369])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "preds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T12:21:22.202644Z",
     "start_time": "2020-09-25T12:21:22.198596Z"
    }
   },
   "outputs": [],
   "source": [
    "from sklearn.metrics import mean_squared_error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T12:21:43.474588Z",
     "start_time": "2020-09-25T12:21:43.470599Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FM MSE: 0.8894\n"
     ]
    }
   ],
   "source": [
    "print('FM MSE: %.4f' % mean_squared_error(y_test, preds))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 分类任务"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T13:50:55.532588Z",
     "start_time": "2020-09-25T13:50:55.528558Z"
    }
   },
   "outputs": [],
   "source": [
    "from sklearn.datasets import make_classification   # 创建一个随机的分类数据集\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import log_loss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T14:16:32.757738Z",
     "start_time": "2020-09-25T14:16:32.725781Z"
    }
   },
   "outputs": [],
   "source": [
    "X, y = make_classification(n_samples=1000, n_features=100, n_clusters_per_class=1) # 1000个训练样本， 100维的数据\n",
    "data = [{v: k for k, v in dict(zip(i, range(len(i)))).items()} for i in X]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T14:16:33.658539Z",
     "start_time": "2020-09-25T14:16:33.653552Z"
    },
    "run_control": {
     "marked": true
    }
   },
   "outputs": [],
   "source": [
    "x_train, x_test, y_train, y_test = train_test_split(data, y, test_size=0.1, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T14:16:34.706565Z",
     "start_time": "2020-09-25T14:16:34.638748Z"
    }
   },
   "outputs": [],
   "source": [
    "v = DictVectorizer()\n",
    "x_train = v.fit_transform(x_train)\n",
    "x_test = v.transform(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T14:16:36.162670Z",
     "start_time": "2020-09-25T14:16:36.157684Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.13546601, -0.44001418,  0.05883419, ...,  0.1106177 ,\n",
       "         2.40164079,  1.06692114],\n",
       "       [-0.95999225, -0.11428069,  1.52518244, ...,  0.34756597,\n",
       "        -1.02474209, -0.89588306],\n",
       "       [ 0.33820952,  0.02851325,  0.44870256, ..., -2.15892132,\n",
       "         0.62617635,  1.42945567],\n",
       "       ...,\n",
       "       [-1.17764912,  0.52031492, -0.58740119, ...,  0.27980417,\n",
       "        -0.17167864, -0.10519347],\n",
       "       [-0.35854756,  0.31230172, -0.19378306, ..., -0.40616573,\n",
       "        -0.10869339, -0.72132468],\n",
       "       [ 1.14339962,  0.1036347 , -0.44070509, ...,  0.94306115,\n",
       "         0.22989284,  1.45309461]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_train.toarray()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T13:51:19.922357Z",
     "start_time": "2020-09-25T13:51:19.919338Z"
    }
   },
   "outputs": [],
   "source": [
    "# 建立模型\n",
    "fm = pylibfm.FM(num_factors=50, num_iter=10, verbose=True, task='classification', initial_learning_rate=0.0001, learning_rate_schedule='optimal')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T13:51:42.579479Z",
     "start_time": "2020-09-25T13:51:20.703753Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Creating validation dataset of 0.01 of training for adaptive regularization\n",
      "-- Epoch 1\n",
      "Training log loss: 2.06286\n",
      "-- Epoch 2\n",
      "Training log loss: 1.77003\n",
      "-- Epoch 3\n",
      "Training log loss: 1.51550\n",
      "-- Epoch 4\n",
      "Training log loss: 1.29592\n",
      "-- Epoch 5\n",
      "Training log loss: 1.10810\n",
      "-- Epoch 6\n",
      "Training log loss: 0.94841\n",
      "-- Epoch 7\n",
      "Training log loss: 0.81340\n",
      "-- Epoch 8\n",
      "Training log loss: 0.70018\n",
      "-- Epoch 9\n",
      "Training log loss: 0.60551\n",
      "-- Epoch 10\n",
      "Training log loss: 0.52595\n"
     ]
    }
   ],
   "source": [
    "fm.fit(x_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T14:02:32.502625Z",
     "start_time": "2020-09-25T14:02:32.497638Z"
    }
   },
   "outputs": [],
   "source": [
    "y_pre = fm.predict(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-25T14:02:57.722991Z",
     "start_time": "2020-09-25T14:02:57.718994Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "validation log loss: 1.3945\n"
     ]
    }
   ],
   "source": [
    "print('validation log loss: %.4f' % log_loss(y_test, y_pre))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
